﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HotelSoft.HotelManagement_DTO;
using System.Collections;


namespace HotelSoft.HotelManagement_DAO
{
    class HoaDonThanhToanDAO
    {
        public ArrayList HoaDonThanhToan()
        {
            ArrayList arrDanhSach = new ArrayList();
            string searchCommand = "SELECT hd.SoHD ,kh.CMND ,kh.HoTenKH ,hd.NgayThanhToan ,hd.TongTien ,nv.MasoNV, nv.HoTenNV ,pdk.SoPDK,pdk.SoPhong"
                                + " FROM KHACHHANG kh ,KHACHHANG_PHIEUDANGKY khpdk ,PHIEUDANGKY pdk ,HOADON_THANHTOAN hd ,NHANVIEN nv "
                                 + "WHERE kh.CMND=khpdk.CMND and khpdk.SoPDK = pdk.SoPDK and pdk.SoPDK = hd.SoPDK and nv.MasoNV = hd.MasoNV";
           
            DataTable ds = Config.AccessHelper.ExecuteQuery(searchCommand);
            HoaDonThanhToanDTO BaoCao;

            int i = 0;
            int iSoDong = ds.Rows.Count;
            int iSoCot = ds.Columns.Count;
            while (i < iSoDong)
            {
                BaoCao = new HoaDonThanhToanDTO();
                BaoCao.m_MaHD = ds.Rows[i]["SoHD"].ToString();
                BaoCao.m_MaKH = ds.Rows[i]["CMND"].ToString();
                BaoCao.m_TenKH = ds.Rows[i]["HoTenKH"].ToString();
                BaoCao.m_NgayThanhToan = ds.Rows[i]["NgayThanhToan"].ToString();
                BaoCao.m_TongTien = float.Parse(ds.Rows[i]["TongTien"].ToString());
                BaoCao.m_MaNV = ds.Rows[i]["MasoNV"].ToString();
                BaoCao.m_TenNVThu = ds.Rows[i]["HoTenNV"].ToString();
                BaoCao.m_MaPDK = ds.Rows[i]["SoPDK"].ToString();
                BaoCao.m_SoPhong = ds.Rows[i]["SoPhong"].ToString();                            
                arrDanhSach.Add(BaoCao);
                i++;
            }
            return arrDanhSach;
        }

        //public ArrayList TimKiemTheoTenKH(string kh)
        //{
        //    ArrayList arrDanhSach = new ArrayList();
        //    string searchCommand = "SELECT hd.SoHD ,kh.CMND ,kh.HoTenKH ,hd.NgayThanhToan ,hd.TongTien ,nv.HoTenNV ,pdk.SoPDK,pdk.SoPhong"
        //                        + " FROM KHACHHANG kh ,KHACHHANG_PHIEUDANGKY khpdk ,PHIEUDANGKY pdk ,HOADON_THANHTOAN hd ,NHANVIEN nv "
        //                         + "WHERE kh.CMND=khpdk.CMND and khpdk.SoPDK = pdk.SoPDK and pdk.SoPDK = hd.SoPDK and nv.MasoNV = hd.MasoNV "
        //                         + " and kh.HoTenKH LIKE N'%"+ kh +"%'";

        //    DataTable ds = Config.AccessHelper.ExecuteQuery(searchCommand);
        //    HoaDonThanhToanDTO BaoCao;

        //    int i = 0;
        //    int iSoDong = ds.Rows.Count;
        //    int iSoCot = ds.Columns.Count;
        //    while (i < iSoDong)
        //    {
        //        BaoCao = new HoaDonThanhToanDTO();
        //        BaoCao.m_MaHD = ds.Rows[i]["SoHD"].ToString();
        //        BaoCao.m_MaKH = ds.Rows[i]["CMND"].ToString();
        //        BaoCao.m_TenKH = ds.Rows[i]["HoTenKH"].ToString();
        //        BaoCao.m_NgayThanhToan = DateTime.Parse(ds.Rows[i]["NgayThanhToan"].ToString());
        //        BaoCao.m_TongTien = float.Parse(ds.Rows[i]["TongTien"].ToString());
        //        BaoCao.m_TenNVThu = ds.Rows[i]["HoTenNV"].ToString();
        //        BaoCao.m_MaPDK = ds.Rows[i]["SoPDK"].ToString();
        //        BaoCao.m_SoPhong = ds.Rows[i]["SoPhong"].ToString();
        //        arrDanhSach.Add(BaoCao);
        //        i++;
        //    }
        //    return arrDanhSach;
        //}

        //public ArrayList TimKiemTheoTienThanhToan(float t)
        //{
        //    ArrayList arrDanhSach = new ArrayList();
        //    string searchCommand = "SELECT hd.SoHD ,kh.CMND ,kh.HoTenKH ,hd.NgayThanhToan ,hd.TongTien ,nv.HoTenNV ,pdk.SoPDK,pdk.SoPhong"
        //                        + " FROM KHACHHANG kh ,KHACHHANG_PHIEUDANGKY khpdk ,PHIEUDANGKY pdk ,HOADON_THANHTOAN hd ,NHANVIEN nv "
        //                         + "WHERE kh.CMND=khpdk.CMND and khpdk.SoPDK = pdk.SoPDK and pdk.SoPDK = hd.SoPDK and nv.MasoNV = hd.MasoNV "
        //                         + "and hd.TongTien =" + t;

        //    DataTable ds = Config.AccessHelper.ExecuteQuery(searchCommand);
        //    HoaDonThanhToanDTO BaoCao;

        //    int i = 0;
        //    int iSoDong = ds.Rows.Count;
        //    int iSoCot = ds.Columns.Count;
        //    while (i < iSoDong)
        //    {
        //        BaoCao = new HoaDonThanhToanDTO();
        //        BaoCao.m_MaHD = ds.Rows[i]["SoHD"].ToString();
        //        BaoCao.m_MaKH = ds.Rows[i]["CMND"].ToString();
        //        BaoCao.m_TenKH = ds.Rows[i]["HoTenKH"].ToString();
        //        BaoCao.m_NgayThanhToan = DateTime.Parse(ds.Rows[i]["NgayThanhToan"].ToString());
        //        BaoCao.m_TongTien = float.Parse(ds.Rows[i]["TongTien"].ToString());
        //        BaoCao.m_TenNVThu = ds.Rows[i]["HoTenNV"].ToString();
        //        BaoCao.m_MaPDK = ds.Rows[i]["SoPDK"].ToString();
        //        BaoCao.m_SoPhong = ds.Rows[i]["SoPhong"].ToString();
        //        arrDanhSach.Add(BaoCao);
        //        i++;
        //    }
        //    return arrDanhSach;
        //}

        public bool SoSanhChuoi(String a, String b)
        {
            String a1 = a.ToLower();
            String b1 = b.ToLower();
            a1 = a1.Trim();
            b1 = b1.Trim();
            if (a1.Contains(b1))
                return true;
            return false;
        }
        public bool SoSanhHD(HoaDonThanhToanDTO dtoHD1, HoaDonThanhToanDTO dtoNHD2)
        {
            bool flag = false;

            if (dtoNHD2.m_MaNV != null)
                if (dtoHD1.m_MaNV.ToUpper() == dtoNHD2.m_MaNV.ToUpper())
                    flag = true;
                else
                    return false;
            if (dtoNHD2.m_TenNVThu != null)
                if (SoSanhChuoi(dtoHD1.m_TenNVThu.ToString(), dtoNHD2.m_TenNVThu.ToString()) == true)
                    flag = true;
                else
                    return false;
            if (dtoNHD2.m_MaKH != null)
                if (dtoHD1.m_MaKH.ToUpper() == dtoNHD2.m_MaKH.ToUpper())
                    flag = true;
                else
                    return false;
            if (dtoNHD2.m_TenKH != null)
                if (SoSanhChuoi(dtoHD1.m_TenKH.ToString(), dtoNHD2.m_TenKH.ToString()) == true)
                    flag = true;
                else
                    return false;

            if (dtoNHD2.m_NgayThanhToan != null)
                if (SoSanhChuoi(dtoHD1.m_NgayThanhToan, dtoNHD2.m_NgayThanhToan) == true)
                    flag = true;
                else
                    return false;

            if (dtoNHD2.m_TongTien != 0)
                if (dtoHD1.m_TongTien.ToString() == dtoNHD2.m_TongTien.ToString())
                    flag = true;
                else
                    return false;

            if (dtoNHD2.m_MaHD != null)
                if (dtoHD1.m_MaHD.ToUpper() == dtoNHD2.m_MaHD.ToUpper())
                    flag = true;
                else
                    return false;
            if (dtoNHD2.m_MaPDK != null)
                if (dtoHD1.m_MaPDK.ToUpper() == dtoNHD2.m_MaPDK.ToUpper())
                    flag = true;
                else
                    return false;
            return flag;
        }

        public ArrayList TraCuuHD(ArrayList dsHD, HoaDonThanhToanDTO dtoHD)
        {
            ArrayList dsKetQuaTraCuu = new ArrayList();
            for (int i = 0; i < dsHD.Count; i++)
            {
                if (SoSanhHD(((HoaDonThanhToanDTO)dsHD[i]), dtoHD) == true)
                    dsKetQuaTraCuu.Add(((HoaDonThanhToanDTO)dsHD[i]));
            }
            return dsKetQuaTraCuu;
        }
    }
}
